<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="/css/common.css" />
		<link rel="stylesheet" type="text/css" href="/js/flatpickr.min.css" />
		<link rel="stylesheet" type="text/css" href="/bootstrap-3.3.7/css/bootstrap.min.css" />
		<link rel="stylesheet" type="text/css" href="/dialog/css/dialog.css" />
		<link rel="stylesheet" type="text/css" href="/css/font-awesome.css" />

        <script src="/js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="/js/jquery.form.js"></script>
        <script src="/js/google-maps.js" type="text/javascript" charset="utf-8"></script>
        <script src="/dialog/js/dialog.js"></script>
        <script src="/js/flatpickr.js"></script>
        <script src="/js/jquery.validate.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="/js/messages_zh.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="/js/common.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<!--顶部导航 -->
		<nav class="topNavItemBox">
			<div id="ItemBox">
				<div class="title">肉品溯源管理系统</div>
			</div>
		</nav>
		<!-- 左侧导航 -->

			<!-- 左侧导航 -->
		<#include "left.ftl" />

		<!--饲料入库查询-->
		<div class="page_main">
			<h3>饲料入库管理</h3><span class="add_link"><a href="feedsInAdd.ftl">新增饲料入库信息></a></span>
			<div class="page_content">
				<h4>饲料入库查询</h4>
				<br /><br />
				<form id="feedsIn_query" method="post" action="FeedsinAction_getFeedsinByCondition">
					<!-- <label>疫苗名称</label><input type="text" name="" id="vaccine_name" class="commonInputtext" /> -->
					<label style="width:100px">开始时间</label><input type="text" name="beginTime" id="start_data" class="commonInputtext" /><span></span>
					<label style="width:100px">结束时间</label><input type="text" name="endTime" id="end_data" class="commonInputtext" /><span></span>
					<label style="width:100px">经手人</label><input type="text" name="storeman" class="commonInputtext" />
					<input type="submit" value="查询" style="display: inline-block; margin-left: 30px;" />
				</form>
				<div>
					<table id="feedsIn_table" class="table" width="100%">
						<thead>
							<tr>
								<th><input id="checkall" type="checkbox" /></th>
								<th width="150">饲料名称</th>
								<th width="145">饲料类型</th>
								<th width="150">供应商</th>
								<!-- 			<th width="120">经手人</th> -->
								<th width="150">入库时间</th>
								<th width="150">生产时间</th>
								<th width="110">保质期(天)</th>
								<th width="70">数量</th>
								<th width="70">单价</th>
								<th width="70">折扣</th>
								<th width="70">总价</th>
								<th width="150">最终总价</th>
								<th width="150"></th>
							</tr>
						</thead>
						<tbody>
							<tr id="no_data">
								<td width="20"></td>
								<td width="150">暂无数据</td>

							</tr>
						</tbody>
					</table>
				</div>
				<input type="button" id="deleteAllchoice" value="删除所选" style="display: none;" />
				<div id="barcon" name="barcon" style="margin-left: 200px;font-size:10px"></div>
			</div>
			<div class="footer">
				<div class="footer_lineone">
					<span id="logo"></span>肉品溯源管理系统 
					<span class="footer_lineoneSpan"><a href="aboutus.html">关于我们</a></span>
					<span class="footer_lineoneSpan"><a target="_blank" href="http://www.cfsiw.com/">食品安全信息</a></span> 
					<span class="footer_lineoneSpan"><a href="suggestionAdd.ftl">联系我们</a></span>
				</div>
				<div class="footer_linetwo">
					 &copy;2017广东技术师范学院
				</div>
			</div>
		</div>

		<script type="text/javascript" charset="UTF-8">
			$(function() {
				//侧边栏初始化
				$(".vertical-nav").verticalnav({
					speed: 400,
					align: "left"
				});

				//时间控制初始化
				$("#start_data").flatpickr();
				$("#end_data").flatpickr();

				//每次传过来的数据
				var re_data; 

				//查询
				$("#feedsIn_query").ajaxForm(function(data) {
					console.log(data);
					re_data = data;

					//去除暂无数据
					$("#feedsIn_table tbody tr").remove();
					//写入表格
					for(var i = 0; i < data.length; i++) {
						
						var tb = $("#feedsIn_table tbody");
						var td1 = '<td><input type="checkbox" class="isCheck" value="' + data[i].inStoreId + '"/></td>';
						var td3 = '<td>' + data[i].feedName + '</td>';
						var td4 = '<td>' + data[i].component + '</td>';
						var td5 = '<td>' + data[i].compName + '</td>';
						//var td6 = '<td>' + data[i].emName + '</td>';
						var td7 = '<td>' + data[i].produceDate.slice(0, 10) + '</td>';
						var td2 = '<td>' + data[i].depositTime.slice(0, 10) + '</td>';
						if(parseInt(data[i].shelfLife) > 0) {
							var td8 = '<td>' + data[i].shelfLife + '</td>';
						} else {
							var td8 = '<td style="color:red">已过期(' + Math.abs(data[i].shelfLife) + '天)</td>';
						}
						var td9 = '<td>' + data[i].quantity + '</td>';
						var td10 = '<td>' + data[i].eachone + '</td>';
						var td11 = '<td>' + data[i].discount + '</td>';
						var td12 = '<td>' + data[i].total + '</td>';
						var td13 = '<td>' + data[i].finalTotal + '</td>';
						var td_last = '<td><input type="button" onclick="updateData(' + i + ')" class="change" value="更改" style="padding:0 5px"/><input type="button" value="删除" onclick="deleteData(' + data[i].inStoreId + ')" style="padding:0 5px" /></td>'
						var tr_add = '<tr id="feedsIn_row' + data[i].inStoreId + '">' + td1 + td3 + td4 + td5 + td7 + td2 + td8 + td9 + td10 + td11 + td12 + td13 + td_last + '</tr>'
						tb.append(tr_add);
					}
					$("#no_data").remove();
					$("#deleteAllchoice").css("display", "block");
					goPage(1,4,"feedsIn_table")
					//console.log($("input[class='change']").val());	
				});
				
				
				//删除

				function deleteByids(delete_data) {
					console.log(delete_data);
					$.dialog({
						type: 'confirm',
						contentHtml: '<p style:"magrin:0 auto">确认删除？</p>',
						onClickOk: function() {
							$.ajax({
								url: "FeedsinAction_deleteFeedsin",
								data: {
									'selectRows': delete_data
								},
								success: function() {
									//弹成功框
									$.dialog({
										type: 'info',
										contentHtml: '<img class="info-icon" src="dialog/images/icon/success.png" alt="删除成功" /><p class="info-text">删除成功</p>',
										autoClose: 2000
									});
									//删除相对应的行
									var chrstr = delete_data.split(",");
									console.log(chrstr);
									console.log($("#feedsIn_row" + chrstr[0]));
									for(i = 0; i < chrstr.length; i++) {
										$("#feedsIn_row" + chrstr[i]).remove();
									}
								}
							});
						}
					});

				}
				deleteData = function(i) {
					var st = i;
					st += ",";
					deleteByids(st);
				}
				$("#deleteAllchoice").click(function() {
					delete_data = get_checkbox_data();
					deleteByids(delete_data);
				});

				function get_checkbox_data() {
					var data_temp = "";
					$("input[class='isCheck']:checked").each(function() { // 遍历选中的checkbox
						data_temp += $(this).val() + ',';
						console.log(data_temp);
					});
					return data_temp;
				}

				//更新
				updateData = function(i) {
					var row0 = '<form id="feedsIn_updateform" action="FeedsinAction_updateFeedsin" method="post"><ul class="changePage500">'
					var row1 = '<li><label class="labelBeforinput">饲料名称</label><input name="feedName" readOnly="true" class="bigInputtext" type="text" value="' + re_data[i].feedName + '" /></li>';
					var row2 = '<li><label class="labelBeforinput">饲料类型</label><input name="component" require class="bigInputtext" type="text" value="' + re_data[i].component + '" /></li>';
					//	var row3 = '<li><label class="labelBeforinput">供应商</label><input name="compName" class="bigInputtext" type="text" value="' + re_data[i].compName + '" /></li>';
					var row4 = '<li><label class="labelBeforinput">经手人</label><input name="storeman" data-rule-isChinese="true" require class="bigInputtext" type="text" value="' + re_data[i].storeman + '"  /></li>';
					var row5 = '<li><label class="labelBeforinput">入库时间</label><input name="depositTime"  data-date-format="Y-m-d" class="bigInputtext" type="text" value="' + re_data[i].depositTime.slice(0, 10) + '" /></li>';
					var row6 = '<li><label class="labelBeforinput">生产日期</label><input name="produceDate" data-date-format="Y-m-d" class="bigInputtext" type="text" value="' + re_data[i].produceDate.slice(0, 10) + '" /></li>';
					var row7 = '<li><label class="labelBeforinput">过期日期</label><input name="expireDate" require class="bigInputtext" type="text" value="' + re_data[i].expireDate.slice(0, 10) + '" /></li>';
					var row8 = '<li><label class="labelBeforinput">数量</label><input name="quantity" data-rule-isIntGtZero="true" require class="bigInputtext" type="text" value="' + re_data[i].quantity + '" /></li>';
					var row9 = '<li><label class="labelBeforinput">单价</label><input name="eachone" data-rule-isIntOrDecimal="true" require class="bigInputtext" type="text" value="' + re_data[i].eachone + '" /></li>';
					var row10 = '<li><label class="labelBeforinput">折扣</label><input name="discount" data-rule-isDecimal="true" require class="bigInputtext" type="text" value="' + re_data[i].discount + '" /></li>';
					var row11 = '<li><label class="labelBeforinput">总价</label><input name="total" readOnly="true" class="bigInputtext" type="text" value="' + re_data[i].total + '" /></li>';
					var row12 = '<li><label class="labelBeforinput">最终总价</label><input name="finalTotal" readOnly="true" class="bigInputtext" type="text" value="' + re_data[i].finalTotal + '" /></li></ul>';
					var row13 = '<input type="hidden" name="inStoreId" value="' + re_data[i].inStoreId + '" />';
					var row14 = '<input type="hidden" name="feeds.feedId" value="' + re_data[i].feedId + '" />'
					var row15 = '<input type="hidden" name="sendMan" value="' + re_data[i].sendMan + '" />'
					//	var row16 = '<input type="hidden" name="storeman" value="' + re_data[i].storeman + '" />'
					var row17 = '<input type="hidden" name="remark" value="' + re_data[i].remark + '" />'
					var row18 = '<input type="hidden" name="areacodeinfor.areaId" value="' + re_data[i].areaId + '" />'
					var row19 = '<input type="hidden" name="oldAmount" value="' + re_data[i].quantity + '" />'
					var row20 = '<input type="hidden" name="compName" value="' + re_data[i].compName + '" />'
					var row21 = '<input type="hidden" name="unit" value="' + re_data[i].unit + '" />'
					var row22 = '<input type="hidden" name="feedId" value="' + re_data[i].feedId + '" />'
					var row23 = '<input type="hidden" name="areaId" value="' + re_data[i].areaId + '" />'
					var rowLast = '</form>'
					var str = row0 + row1 + row2 + row4 + row5 + row6 + row7 + row8 + row9 + row10 + row11 + row12 + row13 + row14 + row15 + row17 + row18 + row19 + row20 + row21 + row22 + row23 + rowLast;

					//弹窗
					$.dialog({
						type: 'confirm',
						titleText: '更改数据',
						contentHtml: str,
						//点击取消
						onClickCancel: function() {
							//关闭计时器
							clearInterval(stopInterval);
						},
						//点击确定
						onClickOk: function() {
							//提交表单
							$("#feedsIn_updateform").ajaxSubmit({
								type: 'post',
								error: function(XMLHttpRequest, textStatus, errorThrown) {
									//弹失败框
									$.dialog({
										type: 'info',
										contentHtml: '<img class="info-icon" src="dialog/images/icon/fail.png" alt="更新失败" /><p class="info-text">更新失败</p>',
										autoClose: 2000
									});
								},
								success: function(data) {
									//弹成功框
									$.dialog({
										type: 'info',
										contentHtml: '<img class="info-icon" src="dialog/images/icon/success.png" alt="更新成功" /><p class="info-text">更新成功</p>',
										autoClose: 2000
									});

									console.log(data);
									//回显
									var row = $("#feedsIn_row" + data.feedsin.inStoreId);
									row.find("td").eq(1).text(data.feedsin.feedName);
									row.find("td").eq(2).text(data.feedsin.component);
									row.find("td").eq(3).text(data.feedsin.compName);
									row.find("td").eq(4).text(data.feedsin.depositTime.slice(0, 10));
									row.find("td").eq(5).text(data.feedsin.produceDate.slice(0, 10));
									var str1 = data.feedsin.expireDate.replace((/-/g, '/'));
									var str2 = data.feedsin.produceDate.replace((/-/g, '/'));
									var expireDate = new Date(str1);
									var produceDate = new Date(str2);
									var timestamp1 = expireDate.getTime();
									var timestamp2 = produceDate.getTime();
									var hm = (timestamp1 - timestamp2) / 1000 / 60 / 60 / 24;
									row.find("td").eq(6).text('已过期(' + hm + ')');
									row.find("td").eq(7).text(data.feedsin.quantity);
									row.find("td").eq(8).text(data.feedsin.eachone);
									row.find("td").eq(9).text(data.feedsin.discount);
									row.find("td").eq(10).text(data.feedsin.total);
									row.find("td").eq(11).text(data.feedsin.finalTotal);
									re_data[i] = data.feedsin;
									//关闭计时器
									clearInterval(stopInterval);
								}

							});

						}
					});
					//计时器控制自动计算
					var stopInterval = setInterval(function() {
						var total = $("input[name='quantity']").val() * $("input[name='eachone']").val();
						var finalTotal = $("input[name='discount']").val() * total;
						console.log($("input[name='discount']").val());
						$("input[name='total']").val(total);
						$("input[name='finalTotal']").val(finalTotal);
					}, 1000);

					//时间控制器

					$("input[name='depositTime']").flatpickr();
					$("input[name='expireDate']").flatpickr();
					$("input[name='produceDate']").flatpickr();
					
					//表单校验
					$("#feedsIn_updateform").validate();
				}

			});
		</script>

	</body>

</html>

